<?php
	include_once('class/berita.php');
	
	$news=new Berita();
	
	//$data=$news->getAllBerita();

	
	$list=$news->getAllBerita();
	$jml_hal=ceil(count($list)/3);
	if($list>=1)
	{
		if(isset($_GET['page'])){
			$hal=$_GET['page'];
			$data=$news->getAllPageBerita($hal);
		}
		else
		{
			$data=$news->getAllPageBerita(0);
		}
	}

		
	if(isset($_POST['simpan_berita'])){
		include_once('class/berita.php');
		$berita=new Berita();
		
		$judul=$_POST['judul_berita'];
		$isi_berita=$_POST['isi_berita'];
	
		$id_username=$_SESSION['login'];
		
		//gambar
		$fileName = $_FILES['gambar_berita']['name'];
		$tmpName = $_FILES['gambar_berita']['tmp_name'];
		$fileSize = $_FILES['gambar_berita']['size'];
		$fileType = $_FILES['gambar_berita']['type'];
		
		if(empty($judul) || empty($isi_berita))
		{
			$error_judulberita="Tidak boleh Kosong";
		}
		 
		elseif(empty($fileName))
		{
			$error_produk="upload File gambar";
		}
		else
		{
			$fp = fopen($tmpName, 'r');
			$gambar = fread($fp, filesize($tmpName));
			$gambar = addslashes($gambar);
			fclose($fp);
										
			$insert=$berita->tambahBerita($judul, $isi_berita, $gambar ,$id_username);
			if($insert==1)
			{
				$success_upload_berita="Berita sukses diupload";
			}
			else
			{
				$error_upload_berita="Gagal upload berita";
			}
		}
			
	}
	
	if(isset($_GET['edit']))
	{
		$no_berita=$_GET['id'];
		
		$brt_edit=$news->getBerita($no_berita);
		
		foreach($brt_edit as $rows_edit){
		
		
?>

		<div id="editBerita" class="editBerita">


	<form class="form-horizontal" method="post" action="#" enctype="multipart/form-data"  >
		<div class="control-group">
			<label class="control-label" for="inputBerita">Judul Berita</label>
				<div class="controls">
					<input type="text" cols="80" name="judul_berita" id="inputBerita" placeholder="Judul Berita" value="<?php echo $rows_edit['judul_berita']; ?>">
				</div>
		</div>
		<div class="control-group">
			<label class="control-label" for="inputBerita">Isi Berita</label>
				<div class="controls">
				<textarea name="isi_berita" cols="100"  rows="20" id="inputBerita" placeholder="Isi Berita"><?php echo $row_edit ['isi_berita'];?> </textarea>
					
				</div>
		</div>
		
		<div class="control-group">
			<label class="control-label" for="inputBerita">Gambar Berita</label>
				<div class="controls">
					<input type="hidden" name="MAX_FILE_SIZE" value="60000">
					<input type="file" name="gambar_berita" id="inputBerita" placeholder="Gambar Berita">
				</div>
		</div>
		<div class="control-group">
			<div class="controls">
				<button type="submit" name="simpan_berita" class="btn" id="simpan_berita">Rekam Berita</button>
				<button type="submit" name="batal" class="btn" id="batal" onclick="batal()">Batal</button>
			</div>
		</div>
	</form>

</div>
<?php	
		}
	}
?>